package com.stox.shared.beans;

import java.util.Comparator;

public final class PropertyDescriptorValueComparator implements
		Comparator<PropertyDescriptor> {

	private static PropertyDescriptorValueComparator instance;
	
	public static PropertyDescriptorValueComparator getInstance(){
		if(null == instance){
			instance = new PropertyDescriptorValueComparator();
		}
		return instance;
	}
	
	private PropertyDescriptorValueComparator() {
		
	}
	
	@Override
	public synchronized int compare(PropertyDescriptor o1, PropertyDescriptor o2) {	
		if(null == o1 && null != o2){
			return -1;
		}else if(null != o1 && null == o2){
			return 1;
		}else if(o1 != null && o2 != null){
			if(Double.class.isAssignableFrom(o1.getType()) ||
					double.class.isAssignableFrom(o1.getType())){
				Double one = Double.parseDouble(String.valueOf(o1.getValue()));
				Double two = Double.parseDouble(String.valueOf(o2.getValue()));
				return one.compareTo(two);
			}
		}
		return 0;
	}

}
